<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<style>
.editPic{
border: 1px solid #cccccc;
    /* padding: 10px; */
    text-align: right;
}
.editpiclink{
       /* position: relative; */
    /* right: 0px; */
    text-align: center;
    /* width: 54px; */
    /* display: inline; */
    font-size: 10px;
    color: red;
    cursor: pointer;
}
</style>
<div style="padding:10px 10px 10px 10px">
	<form id="itemeEditForm" class="itemForm" method="post">
		<input type="hidden" name="id"/>
	    <table style="border-collapse:separate; border-spacing:0px 10px;"> 
	        <tr>
	            <td>商品类目:</td>
	            <td>
	            	<a href="javascript:void(0)" class="easyui-linkbutton selectItemCatedit">选择类目</a>
	            	<input type="hidden" name="cid" style="width: 280px;"></input>	
	            </td>
	        </tr>
	        	        <tr>
	            <td>商品标题:</td>
	            <td><input class="easyui-textbox" type="text" name="name" data-options="required:true" style="width: 280px;"></input></td>
	        </tr>
	        <tr>
	            <td>搜索分类:</td>
	            <td>
	                 <input class="easyui-combobox" name="search" style="width:180px;" data-options="
						valueField:'value',
						textField:'text',
						data:[{value:'1',text:'新生儿玩具'},{value:'2',text:'早教启蒙'},
						{value:'3',text:'仿真过家家'},{value:'4',text:'运动户外'},
						{value:'5',text:'积木'},{value:'6',text:'电动遥控'},
						{value:'7',text:'动漫周边'},{value:'8',text:'木制玩具'},
						{value:'9',text:'益智玩具'},{value:'10',text:'拼装DIY'}]">
	            </td>
	        </tr>	        

	        <tr>
	            <td>性别:</td>
	            <td>
	                 <input class="easyui-combobox" name="sex"  style="width:180px;" data-options="
						valueField:'value',
						textField:'text',
						data:[{value:'1',text:'通用'},{value:'2',text:'女孩'},
						{value:'3',text:'男孩'},{value:'4',text:'成年'}]">
	            </td>
	        </tr>	
	         	
	        <tr>
	            <td>产品来源:</td>
	            <td>
	                 <input class="easyui-combobox" name="source"  style="width:180px;" data-options="
						valueField:'value',
						textField:'text',
						data:[{value:'1',text:'国内精品'},{value:'2',text:'海外精品'},
						{value:'3',text:'自营'}]">
	            </td>
	        </tr>	        
	       	        <tr>
	            <td>年龄下限:</td>
	            <td>
	                <input class="easyui-numberbox" type="text" name="lowage" data-options="required:true" />
	            </td>
	        </tr>
	        <tr>
	            <td>年龄上限:</td>
	            <td>
	                <input class="easyui-numberbox" type="text" name="upage" data-options="required:true" />
	            </td>
	        </tr>	

	        <tr>
	            <td>商品卖点:</td>
	            <td><input class="easyui-textbox" name="intro" data-options="multiline:true,validType:'length[0,150]'" style="height:60px;width: 280px;"></input></td>
	        </tr>
	        	        <tr>
	            <td>视频网址:</td>
	            <td><input class="easyui-textbox" name="video" data-options="multiline:true,validType:'length[0,150]'" style="height:60px;width: 280px;"></input></td>
	        </tr>
	        
	     <tr>
	            <td>进货价:</td>
	            <td><input id="bprice"  class="easyui-numberbox" type="text" name="bpriceView" data-options="prompt:'请输入商品原价',min:1,max:99999999,precision:2,required:true" />单位（元）
	            	<input type="hidden" name="bprice"/>
	            </td>
	        </tr>
	        <tr>
	            <td>参考价:</td>
	            <td><input id="rprice"  class="easyui-numberbox" type="text" name="rpriceView" data-options="prompt:'请输入商品原价',min:1,max:99999999,precision:2,required:true" />单位（元）
	            	<input type="hidden" name="rprice"/>
	            </td>
	        </tr>
	        <tr>
	            <td>定价标准:</td>
	            <td>
					<input type="radio" name="benchmark" value="0" checked="checked">系统</input>
					<input type="radio" name="benchmark" value="1">自定义</input>
	            </td>
	        </tr>
	        <tr>
	            <td>展示价格:</td>
	            <td><input id="sprice" class="easyui-numberbox" type="text" name="spriceView" data-options="precision:2,required:true,readonly:'true'" />单位（元）
	            	<input type="hidden" name="sprice"/>
	            </td>
	        </tr>
	       	<tr>
	            <td>评估价格:</td>
	            <td><input id="pprice" class="easyui-numberbox" type="text" name="ppriceView" data-options="precision:2,required:true,readonly:'true'" />单位（元）
	            	<input type="hidden" name="pprice"/>
	            </td>
	        </tr>
	        <tr>
	            <td>毛利率:</td>
	            <td><input id="margin" class="easyui-numberbox" type="text" name="marginView" data-options="precision:2,required:true,readonly:'true'" />%
	            	<input type="hidden" name="margin"/>
	            </td>
	        </tr>
	        <tr>
	            <td>库存数量:</td>
	            <td><input class="easyui-numberbox" type="text" name="num" data-options="min:100,max:99999999,precision:0,required:true" /></td>
	        </tr>
	        	        <tr>
	            <td>销售数量:</td>
	            <td><input class="easyui-numberbox" type="text" name="shiyong" data-options="min:0,max:99999999,precision:0,readonly:'true'" /></td>
	        </tr>
	        <tr>
	            <td>条形码:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="barcode" data-options="validType:'length[1,30]'" />
	            </td>
	        </tr>
	        <tr>
	            <td>包装尺寸:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="size" />
	            </td>
	        </tr>
	        <tr>
	            <td>材质:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="caizhi" />
	            </td>
	        </tr>
	        <tr>
	            <td>品牌:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="pinpai" />
	            </td>
	        </tr>
	        <tr>
	            <td>重量:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="zhongliang" data-options="min:1,max:99999999,precision:0,required:true"/>单位（克（g））
	            </td>
	        </tr>
	        <tr>
	            <td>适用对象:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="shiyongduixiang" />
	            </td>
	        </tr>
	        <tr>
	            <td>产地:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="productaddress" />
	            </td>
	        </tr>
	        <tr>
	            <td>单位:</td>
	            <td>
	                <input class="easyui-textbox" type="text" name="unit" />
	            </td>
	        </tr>
	        <tr>
	            <td>是否新品:</td>
	            <td>
	                 <input class="easyui-combobox" name="isShow" style="width:180px;" data-options="
						valueField:'value',
						textField:'text',
						data:[{value:'1',text:'新品'},{value:'2',text:'特价'},{value:'3',text:'低价'}]
					">
	            </td>
	        </tr>
	        <tr>
	            <td>是否促销:</td>
	            <td>
	                <input class="easyui-combobox" name="isHot" style="width:180px;" data-options="
						valueField:'value',
						textField:'text',
						data:[{value:'1',text:'热销',},{value:'2',text:'非热销',}]
					">
	            </td>
	        </tr>
	        <tr>
	            <td>商品图片:</td>
	            <td>
	            	<a href="javascript:void(0)" class="easyui-linkbutton" id="editPic" style="line-height: 24px;" >编辑图片</a>
	            	<a href="javascript:void(0)" class="easyui-linkbutton picFileUploadedit">上传图片</a>
	                <input type="hidden" name="photoX" id="hiddenPhotoX"/>
	            </td>
	        </tr>
	        <tr>
	            <td>商品描述:</td>
	            <td>
	                <textarea style="width:800px;height:768px;visibility:hidden;" name="desc"></textarea>
	            </td>
	        </tr>
 	        <tr class="param">
	        	<td>商品规格:</td>
	        	<td class="paramlist">
	        		<li class="addgroupbutton"><a href="javascript:void(0)" class="easyui-linkbutton addGroup">添加分组</a></li>
	        		<div  class="itemParamAddTemplate" style="display: none;">
						<li class="param">
							<ul>
								<li>
									<input class="easyui-textbox" style="width: 200px;" name="group"/>&nbsp;<a href="javascript:void(0)" class="easyui-linkbutton addParam"  title="添加参数" data-options="plain:true,iconCls:'icon-add'"></a>
								</li>
								<li>
									<span>|-------</span><input  style="width: 150px;" class="easyui-textbox" name="param"/>&nbsp;<a href="javascript:void(0)" class="easyui-linkbutton delParam" title="删除" data-options="plain:true,iconCls:'icon-cancel'"></a>						
								</li>
							</ul>
						</li>
					</div>
	        	</td>
	        </tr>
	    </table>
	    <input type="hidden" name="itemParams"/>
	    <input type="hidden" name="itemParamId"/> 
	</form>
	<div style="padding:10px">
	    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="editSubmitForm()" style="width: 100px;height: 30px;line-height: 30px; margin-right: 37px;">修改</a>
	</div>
</div>
<script type="text/javascript">

	var itemEditEditor ;
	$(function(){
		$(".addGroup").click(function(){
			  var temple = $(".itemParamAddTemplate li").eq(0).clone();
			  $(this).parent().parent().append(temple);
			  temple.find(".addParam").click(function(){
				  var li = $(".itemParamAddTemplate li").eq(2).clone();
				  li.find(".delParam").click(function(){
					  $(this).parent().remove();
				  });
				  li.appendTo($(this).parentsUntil("ul").parent());
			  });
			  temple.find(".delParam").click(function(){
				  $(this).parent().remove();
			  });
		 });
		//根据定价标准计算售卖价格  
		//设置商品价格的鼠标离开事件
		$("#bprice").numberbox({  
		  "onChange":function(){  
				//var priceYhView = $('#priceYhView').textbox('getValue');  
				selectDiscountValue();
		  }  
		}); 
		//设置商品价格的鼠标离开事件
		$("#rprice").numberbox({  
		  "onChange":function(){  
				//var priceYhView = $('#priceYhView').textbox('getValue');  
				selectDiscountValue();
		  }  
		}); 
		// 根据价格标准编辑商品价格
		$(":radio").click(function(){
			   if($(this).val() == 0){
				   $('#sprice').textbox('textbox').attr('readonly',true);  
				   $('#pprice').textbox('textbox').attr('readonly',true);  
				   $('#margin').textbox('textbox').attr('readonly',true);  

				   selectDiscountValue()
			   }else{
				   $("#sprice").numberbox("setValue","");
				    $("#pprice").numberbox("setValue","");
				    $("#margin").numberbox("setValue","");
				   $('#sprice').textbox('textbox').attr('readonly',false);  
				   $('#pprice').textbox('textbox').attr('readonly',false);  
				   $('#margin').textbox('textbox').attr('readonly',false);  
			   }			   
			}); 
		//实例化编辑器
		itemEditEditor = TAOTAO.createEditor("#itemeEditForm [name=desc]");
		$("#editPic").click(function(){
			var buttontext = $("#editPic").text()

			if(buttontext=='编辑图片'){
				$("#editPic").text("取消编辑")
				var picslist=""
				$(".picsedit ul li").each(function(i,item){
					var newItem = "<li id='picstaotao" + i +"'>"+item.innerHTML+"<div class='editPic' onclick=deletePictaotao('picstaotao"+ i +"')><div class='editpiclink' >删除</div></div></li>"
					picslist +=  newItem
				})
				$(".picsedit ul").html(picslist)
			}else{
				$("#editPic").text("编辑图片")
				$(".editPic").remove();
			}	
		})
		
		
		
	});
	//查询打折总体打折信息
	function selectDiscountValue(oldPrice){
		//求出个元素的值
		var bprice = $("#bprice").numberbox("getValue");

	    var rprice= $("#rprice").numberbox("getValue");

	    var sprice= $("#sprice").numberbox("getValue");

	    var pprice= $("#pprice").numberbox("getValue");

	    var margin= $("#margin").numberbox("getValue");
		
	    pprice =bprice*1.3
	    if(rprice === 0){
	    	sprice= pprice
	    }else{
	    	sprice = rprice
	    }
	    if(bprice<98){
	    	margin=(sprice*0.9-bprice)/sprice
	    }else if(bprice>=bprice && bprice<199){
	    	
	    	margin=(sprice*0.92-bprice-5)/sprice
	    }if(bprice>=199){
	    	margin=(sprice*0.95-bprice-10)/sprice
	    }
	    $("#sprice").numberbox("setValue",sprice);
	    $("#pprice").numberbox("setValue",pprice);
	    $("#margin").numberbox("setValue",margin*100);
		
	}
	function deletePictaotao(id){
		console.log($("#"+id+" a").attr("href"))
		var path = $("#"+id+" a").attr("href")
		var pic= $("#hiddenPhotoX").val();
		var index = pic.indexOf(path)
		pic = pic.substr(0,index-1) + pic.substr(index+path.length,pic.length-1);
		$("#hiddenPhotoX").val(pic)
		$("#"+id).remove()
	}
	
	function editSubmitForm(){
		/* console.log("111111111fffffffffffffffff11"+$('#itemeEditForm').form('validate'))
		if(!$('#itemeEditForm').form('validate')){
			$.messager.alert('提示','表单还未填写完成!');
			return ;
		} */
		if($("#itemeEditForm [name=cid]").val()==""){
			$.messager.alert('提示','商品类目必须填写');
			return ;
		}
		console.log("11111111111")
		$("#itemeEditForm [name=bprice]").val(eval($("#itemeEditForm [name=bpriceView]").val()) );
		$("#itemeEditForm [name=rprice]").val(eval($("#itemeEditForm [name=rpriceView]").val()) );
		$("#itemeEditForm [name=sprice]").val(eval($("#itemeEditForm [name=spriceView]").val()) );
		$("#itemeEditForm [name=pprice]").val(eval($("#itemeEditForm [name=ppriceView]").val()) );
		$("#itemeEditForm [name=margin]").val(eval($("#itemeEditForm [name=marginView]").val()) );
		itemEditEditor.sync();
		console.log("222222222222")

		var paramJson = addparams();
		
		
		console.log("3333333333333")
		console.log("4444444444444")

		$("#itemeEditForm [name=itemParams]").val(paramJson);
		console.log("5555555555")

		$.post("./item/edit",$("#itemeEditForm").serialize(), function(data){
			console.log("修改商品成功"+ data)
			if(data.status == 200){
				$.messager.alert('提示','修改商品成功!','info',function(){
					$("#itemEditWindow").window('close');
					$("#itemList").datagrid("reload");
				});
			}
		});
	}
	function addparams(){
			var params = [];
			var groups = $("#itemeEditForm [name=group]");
			groups.each(function(i,e){
				var p = $(e).parentsUntil("ul").parent().find("[name=param]");
				var _ps = [];
				p.each(function(_i,_e){
					var _val = $(_e).siblings("input").val();
					if($.trim(_val).length>0){
						_ps.push(_val);						
					}
				});
				var _val = $(e).siblings("input").val();
				if($.trim(_val).length>0 && _ps.length > 0){
					params.push({
						"group":_val,
						"params":_ps
					});					
				}
			});
			//console.log(JSON.stringify(params))
			return JSON.stringify(params)
	
	}
	
	
</script>
